package cn.itcast.mr.combiner;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * Author itcast
 * Date 2021/10/17 14:53
 * Desc TODO
 */
public class BookMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1 todo 读取每行数据
        String line = value.toString();
        //2 todo 判空
        if(StringUtils.isNotEmpty(line)){
            //2.1 todo 判断是什么类型的书
            if(line.contains("入门")){
                context.write(new Text("计算机"),new IntWritable(1));
            }else if(line.contains("史记")|| line.contains("论清王朝")){
                context.write(new Text("历史"),new IntWritable(1));
            }else{
                //2.2 todo 将每种类型赋值1通过context写出去
                context.write(new Text("武功秘籍"),new IntWritable(1));
            }
        }
    }
}
